﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Dataweb.NShape.Designer
{
    public class LanguageResource
    {
        private static Dictionary<String, String> dicMenu = new Dictionary<String, String>();
        private static Dictionary<String, String> dicButton = new Dictionary<String, String>();
        private static Dictionary<String, String> dicLabel = new Dictionary<String, String>();
        private static Dictionary<String, String> dicText = new Dictionary<String, String>();
        private LanguageResource() { }

        static LanguageResource()
        {
            String language = System.Configuration.ConfigurationManager.AppSettings["language"] + "";
            if (language.Trim() == "zh_CN")
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(Dataweb.NShape.Designer.Properties.Resources.AppResource_zh_CN);

                //加载Menu数据
                XmlNodeList nodes = xmlDoc.SelectSingleNode("Resource").SelectSingleNode("Menu").SelectNodes("item");
                if (nodes != null && nodes.Count > 0)
                {
                    foreach (XmlNode item in nodes)
                    {
                        String key = item.Attributes["key"].Value;
                        String value = item.Attributes["value"].Value;

                        dicMenu.Add(key, value);
                    }
                }

                //加载Button数据
                nodes = xmlDoc.SelectSingleNode("Resource").SelectSingleNode("Button").SelectNodes("item");
                if (nodes != null && nodes.Count > 0)
                {
                    foreach (XmlNode item in nodes)
                    {
                        String key = item.Attributes["key"].Value;
                        String value = item.Attributes["value"].Value;

                        dicButton.Add(key, value);
                    }
                }

                //加载Label数据
                nodes = xmlDoc.SelectSingleNode("Resource").SelectSingleNode("Label").SelectNodes("item");
                if (nodes != null && nodes.Count > 0)
                {
                    foreach (XmlNode item in nodes)
                    {
                        String key = item.Attributes["key"].Value;
                        String value = item.Attributes["value"].Value;

                        dicLabel.Add(key, value);
                    }
                }

                //加载Text数据
                nodes = xmlDoc.SelectSingleNode("Resource").SelectSingleNode("Text").SelectNodes("item");
                if (nodes != null && nodes.Count > 0)
                {
                    foreach (XmlNode item in nodes)
                    {
                        String key = item.Attributes["key"].Value;
                        String value = item.Attributes["value"].Value;

                        dicText.Add(key, value);
                    }
                }
            }
        }


        /// <summary>
        /// 获取菜单项文字
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static String GetValue(ToolStripMenuItem menu, String key)
        {
            String value = "";
            if (false == dicMenu.TryGetValue(key, out value))
            {
                value = key;
            }

            return value;
        }
        /// <summary>
        /// 获取Button文字
        /// </summary>
        /// <param name="button"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static String GetValue(Button button, String key)
        {
            String value = "";
            if (false == dicButton.TryGetValue(key, out value))
            {
                value = key;
            }

            return value;
        }
        /// <summary>
        /// 获取Label文字
        /// </summary>
        /// <param name="label"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static String GetValue(Label label, String key)
        {
            String value = "";
            if (false == dicLabel.TryGetValue(key, out value))
            {
                value = key;
            }

            return value;
        }

        /// <summary>
        /// 获取菜单项文字
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static void SetValue(ToolStripMenuItem menu, String key)
        {
            menu.Text = GetValue(menu, key);
        }
        /// <summary>
        /// 获取Button文字
        /// </summary>
        /// <param name="button"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static void SetValue(Button button, String key)
        {
            button.Text = GetValue(button, key);
        }
        /// <summary>
        /// 获取Label文字
        /// </summary>
        /// <param name="label"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static void SetValue(Label label, String key)
        {
            label.Text = GetValue(label, key);
        }

        /// <summary>
        /// 获取普通文本文字
        /// </summary>
        /// <param name="text"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static String GetValue(String key)
        {
            String value = "";
            if (false == dicText.TryGetValue(key, out value))
            {
                value = key;
            }

            return value;
        }


        public static void LoadResource(Control ctl)
        {
            if (ctl.Controls.Count > 0)
            {
                foreach (Control item in ctl.Controls)
                {
                    if (item is MenuStrip)
                    {
                        MenuStrip ctlConvert = item as MenuStrip;
                        foreach (ToolStripItem menuItem in ctlConvert.Items)
                        {
                            if (menuItem is ToolStripMenuItem)
                            {
                                ToolStripMenuItem temp = menuItem as ToolStripMenuItem;
                                SetValue(temp, temp.Text);
                                LoadResource(temp);
                            }
                        }
                    }
                    else if (item is Button)
                    {
                        Button ctlConvert = item as Button;
                        SetValue(ctlConvert, ctlConvert.Text);
                    }
                    else if (item is Label)
                    {
                        Label ctlConvert = item as Label;
                        SetValue(ctlConvert, ctlConvert.Text);
                    }
                    else
                    {
                        LoadResource(item);
                    }
                }
            }
        }

        private static void LoadResource(ToolStripMenuItem menu)
        {
            if (menu.HasDropDownItems)
            {
                foreach (ToolStripItem menuItem in menu.DropDownItems)
                {
                    if (menuItem is ToolStripMenuItem)
                    {
                        ToolStripMenuItem temp = menuItem as ToolStripMenuItem;
                        SetValue(temp, temp.Text);
                        LoadResource(temp);
                    }
                }
            }
        }


    }
}
